home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- *
- * PROJECT: SampleHack
- * FILE: SampleHack
- *
- * DESCRIPTION:
- * A small, simple hack for demonstration at MacHack '99.
- * Rip it apart, make something new out of it, and put it back together.
- * All before the hack contest...
- *
- **********************************************************************/
-
- #include <Pilot.h>
- #include <SysEvtMgr.h>
-
- /*
- List of Stuff:
- • Make a routine with the same parameters & return type as the
- one you're patching. Here its "MySysHandleEvent".
- • Make it the "Entry Point" for the code resource ("68k Target" settings)
- • Put the trap ID of the trap you're patching in the "PalmRez Post Linker"
- settings panel. Get this from the hacktrap.txt file.
- • Change the name of the hack ('tAIN' resource ID 3000)
- • Change the about box ('tFRM' ID 3000 - edit this with Constructor for PalmOS)
- */
-
-
- /***********************************************************************
- *
- * Global variables
- *
- ***********************************************************************/
-
- /*
- We allocate a memory chunk to store our globals in and keep the ptr
- stored in a feature. The PalmOS FeatureMgr is similar to MacOS's Gestalt.
-
- Here is the structure:
- */
-
- typedef struct
- {
- short stringPos;
- }PatchGlobals, *PatchGlobalsPtr;
-
- /***********************************************************************
- *
- * Internal Constants
- *
- ***********************************************************************/
- #define kHackFileCreator 'DARK'
- #define kHackCodeID 1000
- #define kGlobalsPtrID 1100
-
- #define kTargetString "MacHack"
-
-
- /***********************************************************************
- *
- * Function Prototypes
- *
- ***********************************************************************/
-
- extern "C" Boolean MySysHandleEvent(EventPtr eventP);
- PatchGlobalsPtr GetGlobals(void);
- void ScreenSaver(void);
-
-
- void ScreenSaver(void)
- {
- RectangleType screenRect = {{0,0},{160,160}};
- DmOpenRef db;
- Handle pictHandle;
- BitmapPtr pictPtr;
- CustomPatternType pattern = { 0xffff, 0xffff, 0xffff, 0xffff};
-
- WinHandle win = WinGetActiveWindow();
- Word error;
- WinHandle off = WinCreateOffscreenWindow(160, 160, screenFormat, &error);
- WinCopyRectangle(win, off, &screenRect, 0, 0, scrCopy);
-
- WinSetPattern(pattern);
- WinFillRectangle(&screenRect, 0);
-
-
- db = DmOpenDatabaseByTypeCreator('HACK', kHackFileCreator, dmModeReadOnly);
-
- //SndPlaySmfResource('Midi', 128, prefSysSoundVolume);
-
- pictHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 128));
- pictPtr = static_cast<BitmapPtr>(MemHandleLock(pictHandle));
- WinDrawBitmap(pictPtr, 50, 50);
-
- Handle middleHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 128));
- BitmapPtr middlePtr = static_cast<BitmapPtr>(MemHandleLock(middleHandle));
-
- Handle downHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 129));
- BitmapPtr downPtr = static_cast<BitmapPtr>(MemHandleLock(downHandle));
-
- Handle upHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 130));
- BitmapPtr upPtr = static_cast<BitmapPtr>(MemHandleLock(upHandle));
-
- BitmapPtr states[4] = {
- middlePtr,
- downPtr,
- middlePtr,
- upPtr,
- };
-
- int x1 = 160;
- int y1 = 10;
- int x2 = 160;
- int y2 = 60;
-
- for (int i = 0; i != 254; ++i) {
- WinFillRectangle(&screenRect, 0);
- WinDrawBitmap(states[i % 4], x1, y1);
- WinDrawBitmap(states[(i + 1) % 4], x2, y2);
- SysTaskDelay(10);
- x1 -= 2;
- y1 += 1;
- x2 -= 4;
- y2 += 2;
- if (x1 < 0) {
- x1 = 160;
- }
- if (x1 > 160) {
- x1 = 0;
- }
- if (x2 < 0) {
- x2 = 160;
- }
- if (x2 > 160) {
- x2 = 0;
- }
- if (y1 < 0) {
- y1 = 160;
- }
- if (y1 > 160) {
- y1 = 0;
- }
- if (y2 < 0) {
- y2 = 160;
- }
- if (y2 > 160) {
- y2 = 0;
- }
-
- }
-
- MemHandleUnlock(upHandle);
- MemHandleUnlock(downHandle);
- MemHandleUnlock(middleHandle);
- DmReleaseResource(upHandle);
- DmReleaseResource(downHandle);
- DmReleaseResource(middleHandle);
-
- DmCloseDatabase(db);
-
- WinEraseRectangle(&screenRect, 0);
- WinCopyRectangle(off, win, &screenRect, 0, 0, scrCopy);
- WinDeleteWindow(off, false);
-
-
- }
-
-
-
- /*
- This routine is theErr SysUIAppSwitch(UInt cardNo, LocalID dbID, Word cmd, Ptr cmdPBP)
- SYS_TRAP(sysTrapSysUIAppSwitch);
-
- Err SysCurAppDatabase(UIntPtr cardNoP, LocalID* dbIDP)
- SYS_TRAP(sysTrapSysCurAppDatabase);
- entry point to the code resource and will be
- called instead of SysHandleEvent. It watches for keyDown events,
- and if the user enters "MacHack", it will flash the screen.
-
- If you want to patch a different trap, replace this routine with your own.
- Don't forget to update the "Entry Point" field in the "68K Target" settings
- panel to use the new routine!
-
- To tell HackMaster which trap to install your patch on, you must enter the
- trap ID in the "Trap IDs" field of the "PalmRez Post Linker" settings panel.
- See the file "hacktrap.txt" for a list of traps and their ID's.
- */
- Boolean MySysHandleEvent(EventPtr eventP)
- {
- Boolean (*oldtrap)(EventPtr); // procedure pointer to old trap;
- DWord temp; // for the feature manager call type checking
- Boolean handled = false;
- //PatchGlobalsPtr globalsP;
- Err err;
-
- // Get the address of the old routine from HackMaster:
- err = FtrGet(kHackFileCreator,kHackCodeID,&temp);
- ErrFatalDisplayIf(err, "can't get real trap address");
- oldtrap=(Boolean (*)(EventPtr))temp;
-
- switch (eventP -> eType) {
- case keyDownEvent:
- if ((eventP -> data.keyDown.chr == vchrAutoOff) && (eventP -> data.keyDown.modifiers & commandKeyMask)) {
-
- ScreenSaver();
- EvtResetAutoOffTimer();
-
- } else if ((eventP -> data.keyDown.chr == vchrRonamatic) && (eventP -> data.keyDown.modifiers & commandKeyMask)) {
- ScreenSaver();
- EvtResetAutoOffTimer();
- }
- break;
- }
-
- if (!handled) {
- handled = oldtrap(eventP);
- }
-
- return handled;
- }
-
- #ifdef SILLY
- UInt cardNo;
- LocalID dbID;
- if (!SysCurAppDatabase(&cardNo, &dbID)) {
- SysUIAppSwitch(cardNo, dbID, sysAppLaunchCmdNormalLaunch, NULL);
- }
- #endif
-
- /*
- Function to retrieve our globals from a feature.
- Creates & intializes them if necessary.
- Returns ptr to globals, or 0 if something goes awry.
- */
- PatchGlobalsPtr GetGlobals(void)
- {
- PatchGlobalsPtr globalsP;
- Err err;
-
- err = FtrGet(kHackFileCreator, kGlobalsPtrID, (unsigned long*)&globalsP);
-
- // Create & initialize our globals if they don't exist yet.
- if(err == ftrErrNoSuchFeature)
- {
- globalsP = static_cast<PatchGlobalsPtr>(MemPtrNew(sizeof(PatchGlobals)));
-
- if(globalsP != 0)
- {
- // Set its owner to 0 so it isn't automatically freed
- err = MemPtrSetOwner(globalsP, 0);
- ErrFatalDisplayIf(err, "MemPtrSetOwner failed");
-
- // initialize globals structure:
- globalsP->stringPos = 0;
-
-
- // Save our gloabls ptr in a feature so we can use it next time
- FtrSet(kHackFileCreator, kGlobalsPtrID, (unsigned long)globalsP);
-
- // clear error so we know everything's okay.
- err = 0;
- }
- }
-
- // if we got our globals (one way or another)
- // then return them.
- if(err == 0) return globalsP;
-
- return 0;
- }
-
-